<template>
  <div class="container" ref="mainCon">
    <topTitle
      :titleText="titleText"
      :titleExplain="titleExplain"
      :pathUrl="pathUrl"
      :btnText="btnText"
      :isShowBtn="isShowBtn"
      :oldUrl="oldUrl"
    ></topTitle>
    <div class="mainCon">
      <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
        <template v-if="isJC">
          <el-form :inline="true" :model="filterField" ref="searchConditions">
            <el-col :lg="8" :xl="6">
              <el-form-item label="信息类型" class="row-padding-bottom">
                <el-select
                  v-model="filterField.type"
                  clearable
                  filterable
                  placeholder="请选择"
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in typeOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item
                label="发布状态"
                class="row-padding-bottom"
                v-if="isJC"
              >
                <el-select
                  v-model="filterField.status"
                  clearable
                  filterable
                  placeholder="请选择"
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in statusOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <template v-if="advanced">
              <el-col :lg="8" :xl="6">
                <el-form-item
                  label="通报范围"
                  class="row-padding-bottom"
                  v-if="isJC"
                >
                  <el-select
                    v-model="filterField.range"
                    clearable
                    filterable
                    placeholder="请选择"
                    :popper-append-to-body="false"
                  >
                    <el-option
                      v-for="item in rangeOptions"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :xl="6">
                <el-form-item label="发布时间" class="row-padding-bottom">
                  <timeSelection
                    @getStartTimeValue="getStartTimeValue"
                    @getEndTimeValue="getEndTimeValue"
                  ></timeSelection>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :xl="6">
                <el-form-item label="标题" class="row-padding-bottom">
                  <el-input
                    v-model="filterField.title"
                    placeholder="请输入标题"
                    clearable
                  />
                </el-form-item>
              </el-col>
              <!-- <el-form-item>
                <el-button type="primary" icon="el-icon-search" class="searchBut btn_class" @click="searchList">查询</el-button>
            </el-form-item>-->
              <!-- <el-form-item>
                <el-button
                type="primary"
                icon="icon iconfont iconchazhao1"
                @click="searchList"
                class="commonBtn"
                ></el-button>
            </el-form-item> -->
            </template>
            <el-col :lg="8" :xl="6">
              <el-form-item label="">
                <a
                  @click="toggleAdvanced"
                  style="margin-left: 40px; color: #409eff; cursor: pointer"
                  class="btnColor"
                >
                  {{ advanced ? '收起' : '展开' }}
                  <i
                    :class="
                      advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'
                    "
                  ></i>
                </a>
                <el-button
                  type="primary"
                  icon="icon iconfont iconchazhao1"
                  @click="searchList"
                  class="searchBtn"
                >
                  <span>搜索</span>
                </el-button>
              </el-form-item>
              <el-form-item>
                <exportBtn
                  :dataLen="tableList.length"
                  :filterField="filterField"
                  :api="api"
                ></exportBtn>
              </el-form-item>
            </el-col>
          </el-form>
        </template>
        <template v-if="!isJC">
          <el-form :inline="true" :model="filterField" ref="searchConditions">
            <el-col :lg="8" :xl="6">
              <el-form-item label="信息类型" class="row-padding-bottom">
                <el-select
                  v-model="filterField.type"
                  clearable
                  filterable
                  placeholder="请选择"
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in typeOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item
                label="发布状态"
                class="row-padding-bottom"
                v-if="isJC"
              >
                <el-select
                  v-model="filterField.status"
                  clearable
                  filterable
                  placeholder="请选择"
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in statusOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :lg="8" :xl="6">
              <el-form-item
                label="通报范围"
                class="row-padding-bottom"
                v-if="isJC"
              >
                <el-select
                  v-model="filterField.range"
                  clearable
                  filterable
                  placeholder="请选择"
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in rangeOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :lg="8" :xl="6">
              <el-form-item label="发布时间" class="row-padding-bottom">
                <timeSelection
                  @getStartTimeValue="getStartTimeValue"
                  @getEndTimeValue="getEndTimeValue"
                ></timeSelection>
              </el-form-item>
            </el-col>
            <template v-if="advanced">
              <el-col :lg="8" :xl="6">
                <el-form-item label="标题" class="row-padding-bottom">
                  <el-input
                    v-model="filterField.title"
                    placeholder="请输入标题"
                    clearable
                  />
                </el-form-item>
              </el-col>
              <!-- <el-form-item>
                <el-button type="primary" icon="el-icon-search" class="searchBut btn_class" @click="searchList">查询</el-button>
            </el-form-item>-->
              <!-- <el-form-item>
                <el-button
                type="primary"
                icon="icon iconfont iconchazhao1"
                @click="searchList"
                class="commonBtn"
                ></el-button>
            </el-form-item> -->
            </template>
            <el-col :lg="8" :xl="6">
              <el-form-item label="">
                <a
                  @click="toggleAdvanced"
                  style="margin-left: 40px; color: #409eff; cursor: pointer"
                  class="btnColor"
                >
                  {{ advanced ? '收起' : '展开' }}
                  <i
                    :class="
                      advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'
                    "
                  ></i>
                </a>
                <el-button
                  type="primary"
                  icon="icon iconfont iconchazhao1"
                  @click="searchList"
                  class="searchBtn"
                >
                  <span>搜索</span>
                </el-button>
              </el-form-item>
              <el-form-item>
                <exportBtn
                  :dataLen="tableList.length"
                  :filterField="filterField"
                  :api="api"
                ></exportBtn>
              </el-form-item>
            </el-col>
          </el-form>
        </template>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <el-table
          ref="tableBox1"
          @sort-change="sortChange"
          :max-height="tableHeight"
          :data="tableList"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          v-loading="loading"
          stripe
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column prop="title" label="标题" width="230">
            <template slot-scope="scope">
              <div
                @click="showInfor(scope.$index, scope.row)"
                class="fontColor"
                v-if="isShowDetail"
              >
                <copyText
                  :tableIndex="scope.$index"
                  :copyTextName="scope.row.title"
                  :columnName="'title'"
                ></copyText>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="infoTypeDesc"
            label="信息类型"
            align="center"
            :formatter="formatTd"
            min-width="120"
          ></el-table-column>
          <el-table-column
            prop="infoRangeDesc"
            label="通报范围"
            align="center"
            :formatter="formatTd"
            min-width="140"
            v-if="isJC"
          ></el-table-column>
          <el-table-column
            prop="createTime"
            sortable="custom"
            label="添加/发布时间"
            align="center"
            min-width="140"
          >
            <template slot-scope="scope">{{
              scope.row.createTime || '--'
            }}</template>
          </el-table-column>
          <el-table-column
            prop="author"
            label="发布人"
            align="center"
            :formatter="formatTd"
            min-width="120"
          ></el-table-column>
          <el-table-column
            prop="statusDesc"
            label="发布状态"
            align="center"
            :formatter="formatTd"
            min-width="120"
            v-if="isJC"
          ></el-table-column>
          <!--<el-table-column prop="handleCreateTime" label="阅读量" width="100"></!--<el-table-column>-->
          <el-table-column
            label="操作"
            align="left"
            width="190"
            fixed="right"
            v-if="isJC"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.statusDesc === '已发布'">
                <el-link
                  type="primary"
                  v-if="hasUpdate"
                  @click="inforHandle(scope.$index, scope.row)"
                  >撤回</el-link
                >
                <span v-else>--</span>
              </span>
              <span v-if="scope.row.statusDesc != '已发布'">
                <el-link
                  type="primary"
                  v-if="hasUpdate"
                  @click="inforHandle(scope.$index, scope.row)"
                  >编辑</el-link
                >
                <span v-else>--</span>
              </span>
              <span v-if="scope.row.statusDesc != '待发布'">
                <el-link
                  type="primary"
                  v-if="isShowDetail"
                  @click="viewReading(scope.$index, scope.row)"
                  >查看阅读情况</el-link
                >
                <span v-else>--</span>
              </span>
              <!-- 已撤回新增删除操作 -->
              <span v-if="scope.row.statusDesc === '已撤回'">
                <el-link
                  type="primary"
                  v-if="hasWithdraw"
                  @click="deleteHandle(scope.$index, scope.row)"
                  >删除</el-link
                >
                <span v-else>--</span>
              </span>
              <!-- <el-button size="small" type="primary" class="revoBtn" v-if="scope.row.status==='已发布'" @click="inforHandle(scope.$index, scope.row)" >
                        撤回
                    </el-button>
                    <el-button size="small" type="primary" class="saveBtn" v-if="scope.row.status!='已发布'" @click="inforHandle(scope.$index, scope.row)" >
                    编辑
                    </el-button>-->
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关信息"></emptyTable>
          </div>
        </el-table>

        <Pagination
          ref="pagination"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
          @getpageSize="getpageSize"
          @getpageNo="getpageNo"
        ></Pagination>
      </div>
    </div>
  </div>
</template>
<script>
import scrollTable from '@/mixin/scrollTable.js'
export default {
  name: 'inforList',
  mixins: [scrollTable],
  data() {
    return {
      titleText: '政策通知',
      advanced: false,
      titleExplain: '',
      pathUrl: '/home/notice/addNotification',
      btnText: '信息添加',
      isShow: false, //列表加载
      loading: true,
      //下拉框数据
      //查询条件数据封装
      statusOptions: [],
      typeOptions: [],
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        },
      },
      //表格数据
      tableList: [],
      requestUrl: 'api-i/info/expand/listbypage',
      dateValue: [],
      //搜索条件
      filterField: {
        sysAppCode: localStorage.getItem('judgeRoleCode'),
        status: '',
        type: '',
        title: '',
        startTime: '',
        endTime: '',
        range: '',
      },
      pageSize: 20,
      pageNo: 1,
      isShowBtn: false,
      hasWithdraw: false,
      hasUpdate: false,
      rangeOptions: [
        { name: '建筑', code: '01' },
        { name: '非建', code: '03' },
        { name: '全部', code: '05' },
      ],
      oldUrl: '/home/notice/inforList',
      isJC: '',
      isShowDetail: '',
      api: '',
    }
  },
  created() {
    //是否是监察
    this.isJC = localStorage.getItem('loginType') == 'JC' ? true : false
    if (
      this.$store.state.userType == 'PROJECT_ADMINISTRATOR' ||
      this.$store.state.userType == 'LABOR_ADMINISTRATOR'
    ) {
      this.filterField.status = '02'
      this.filterField.type = '05'
      this.filterField.reveiveUserType = 'PROJECT'
    }
    if (this.$store.state.userType === 'COMPANY') {
      this.filterField.status = '02'
      this.filterField.type = '05'
      this.filterField.reveiveUserType = 'COMPANY'
    }
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
    this.getApi()
    //初始化选择列表
    this.getStatusData('api-bs/base/dicitem/findbytype', {
      type: 'INFO_STATUS',
    }) //发布状态下拉框
    this.getTypeData('api-bs/base/dicitem/findbytype', {
      type: 'INFO_TYPE',
    }) //信息类别下拉框

    // 判断按钮权限
    this.isShowBtn =
      this.isJC &&
      (this.$store.state.buttonLimitsArr.indexOf('policyNotice:add') > -1 ||
        this.$store.state.buttonLimitsArr.indexOf('inforList:add') > -1 ||
        this.$store.state.buttonLimitsArr.indexOf('policyNoticeSGF:add') > -1 ||
        this.$store.state.buttonLimitsArr.indexOf('inforListSGF:add') > -1 ||
        this.$store.state.buttonLimitsArr.indexOf('policyNoticeJSF:add') > -1 ||
        this.$store.state.buttonLimitsArr.indexOf('inforListJSF:add') > -1)
    this.hasWithdraw =
      this.$store.state.buttonLimitsArr.indexOf('policyNotice:delete') > -1 ||
      this.$store.state.buttonLimitsArr.indexOf('inforList:delete') > -1 ||
      this.$store.state.buttonLimitsArr.indexOf('policyNoticeSGF:delete') >
        -1 ||
      this.$store.state.buttonLimitsArr.indexOf('inforListSGF:delete') > -1
    this.$store.state.buttonLimitsArr.indexOf('policyNoticeJSF:delete') > -1 ||
      this.$store.state.buttonLimitsArr.indexOf('inforListJSF:delete') > -1
    this.hasUpdate =
      this.$store.state.buttonLimitsArr.indexOf('policyNotice:update') > -1 ||
      this.$store.state.buttonLimitsArr.indexOf('inforList:update') > -1 ||
      this.$store.state.buttonLimitsArr.indexOf('policyNoticeSGF:update') >
        -1 ||
      this.$store.state.buttonLimitsArr.indexOf('inforListSGF:update') > -1
    this.$store.state.buttonLimitsArr.indexOf('policyNoticeJSF:update') > -1 ||
      this.$store.state.buttonLimitsArr.indexOf('inforListJSF:update') > -1
    this.isShowDetail =
      this.$store.state.buttonLimitsArr.indexOf('policyNotice:detail') > -1 ||
      this.$store.state.buttonLimitsArr.indexOf('inforList:detail') > -1 ||
      this.$store.state.buttonLimitsArr.indexOf('policyNoticeSGF:detail') >
        -1 ||
      this.$store.state.buttonLimitsArr.indexOf('inforListSGF:detail') > -1
    this.$store.state.buttonLimitsArr.indexOf('policyNoticeJSF:detail') > -1 ||
      this.$store.state.buttonLimitsArr.indexOf('inforListJSF:detail') > -1
  },
  activated() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
    })
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    /**
     * @description: 获取开始日期
     * @param {*} urls
     * @param {*} datas
     */
    getStartTimeValue(val) {
      if (val) {
        this.filterField.startTime = val
      } else {
        this.filterField.startTime = ''
      }
    },
    /**
     * @description: 获取结束日期
     * @param {*} urls
     * @param {*} datas
     */
    getEndTimeValue(val) {
      if (val) {
        this.filterField.endTime = val
      } else {
        this.filterField.endTime = ''
      }
    },
    getStatusData(urls, datas) {
      this.axios({
        method: 'post',
        url: urls,
        data: datas,
      })
        .then((resp) => {
          this.statusOptions = resp.data.result
        })
        .catch((resp) => {})
    },
    getTypeData(urls, datas) {
      this.axios({
        method: 'post',
        url: urls,
        data: datas,
      })
        .then((resp) => {
          this.typeOptions = resp.data.result
        })
        .catch((resp) => {})
    },
    inforHandle(index, row) {
      if (row.statusDesc === '待发布') {
        this.toEdit(index, row)
      } else if (row.statusDesc === '已发布') {
        this.toRecall(index, row)
      } else if (row.statusDesc === '已撤回') {
        this.toEdit(index, row)
      }
    },
    /**
     * @description: 查看阅读情况
     */
    viewReading(index, row) {
      this.$router.push({
        path: '/home/notice/viewReading',
        name: 'viewReading',
        query: {
          infoId: row.id,
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/notice/inforList',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    // 编辑
    toEdit(index, row) {
      this.$router.push({
        path: '/home/notice/addNotification',
        name: 'addNotification',
        query: {
          infoId: row.id,
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/notice/inforList',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    // 点击撤回信息
    toRecall(index, row) {
      this.$confirm(
        '信息撤回后, 所有人员将无法查看,是否确认撤回?',
        '撤回信息',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      )
        .then(() => {
          this.recall(index, row)
        })
        .catch(() => {})
    },
    // 确认撤回
    recall(index, row) {
      this.axios({
        method: 'post',
        url: 'api-i/info/expand/withdraw',
        data: {
          infoId: row.id,
        },
      })
        .then(
          (res) => {
            if (!res.data.hasError) {
              this.$message({
                type: 'success',
                message: '撤回成功!',
              })
              this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
            } else {
              this.$message({
                type: 'error',
                message: res.data.errorMsg,
              })
            }
          },
          (response) => {
            this.$message({
              type: 'error',
              message: '数据请求失败',
            })
          }
        )
        .catch((res) => {})
    },
    //删除
    deleteHandle(index, row) {
      this.$confirm('是否删除本条通知?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.delNotice(row)
        })
        .catch(() => {})
    },
    delNotice(row) {
      this.axios({
        method: 'post',
        url: 'api-i/info/expand/delete',
        data: {
          id: row.id,
        },
      })
        .then(
          (response) => {
            if (!response.data.hasError) {
              this.$message({
                message: '删除成功',
                type: 'success',
              })
              this.$refs.pagination.getTableListData()
            } else {
              this.$message({
                message: response.data.errorMsg,
                type: 'error',
              })
            }
          },
          (response) => {}
        )
        .catch((error) => {})
    },
    // 获取列表数据
    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },
    showInfor(index, row) {
      this.$router.push({
        path: '/home/notice/inforDetail',
        name: 'inforDetail',
        query: {
          id: row.id,
          status: this.filterField.status,
          title: this.filterField.title,
          type: this.filterField.type,
          startTime: this.filterField.startTime,
          endTime: this.filterField.endTime,
          range: '',
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/notice/inforList',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    getApi() {
      let loginType = localStorage.getItem('loginType')
      let judgeRoleCode = localStorage.getItem('judgeRoleCode')
      if (judgeRoleCode === 'company') {
        if (loginType === 'JC') {
          this.api = 'api-s/report/export_JC_NC_POLICY_INFO'
        } else if (loginType === 'QY') {
          this.api = 'api-s/report/export_QY_NC_POLICY_INFO'
        }
      } else {
        if (loginType === 'JC') {
          this.api = 'api-s/report/export_JC_CS_POLICY_INFO'
        } else if (loginType === 'QY') {
          this.api = 'api-s/report/export_QY_POLICY_INFO'
        } else if (loginType === 'XM') {
          this.api = 'api-s/report/export_XM_POLICY_INFO'
        } else if (loginType === 'BANK') {
          this.api = 'api-s/report/export_YH_POLICY_INFO'
        }
      }
    },
  },
}
</script>

<style scoped lang="less">
.mainCon {
  top: 198px;
}
.revoBtn {
  background-color: #909399;
  border-color: #909399;
  color: #fff;
}
.revoBtn:hover {
  background-color: #909399;
  border-color: #909399;
  color: #fff;
  opacity: 0.8;
}
.a_link {
  color: #409eff;
  cursor: pointer;
}
.container /deep/ .topTitle .btn {
  // top: 16px;
  // /deep/.el-table td{
  //     text-align: left;
  // }
}
/deep/.el-table__body tbody .el-table__row td:nth-child(1) {
  text-align: left !important;
}
@media screen and (min-width: 1280px) and (max-width: 1366px) {
  .searchInputWidth /deep/ .el-form-item__label {
    width: 70px;
  }
}
@import '../../../static/css/filterArea.less';

.fontColor {
  color: #409eff;
  cursor: pointer;
}
</style>
